package org.znxs.comfriend.utils;

import android.util.Log;

import org.znxs.comfriend.models.User;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Random;

public class UserUtils {

    private static final String[] NICKNAMES = {
            "Angelo Bartoletti III",
            "Mrs.Leah Aufderhar",
            "Dwight Bauch IV",
            "Daisy Hahn",
            "Darrin Goyette",
            "Francis Fay",
            "Myrtle Kassulke",
            "Luke Gleichner III",
            "Kim Emmerich",
            "Ada Hayes DVM",
            "Dr.Velma Hermann",
            "Mrs.Josephine Wiza",
            "Ken Moen",
            "June Green",
            "Dewey D"
    };

    private static final String[] GENDERS = {"男", "女"};
    private static final String[] AVATAR_URLS = {
            "https://images.xxapi.cn/images/head/0035129704.jpg",
            "https://images.xxapi.cn/images/head/9199988744.jpg",
            "https://images.xxapi.cn/images/head/0948273779.jpg",
            "https://images.xxapi.cn/images/head/6437048042.jpg",
            "https://images.xxapi.cn/images/head/4398620744.jpg",
            "https://images.xxapi.cn/images/head/8695043755.jpg",
            "https://images.xxapi.cn/images/head/3347472359.jpg",
            "https://images.xxapi.cn/images/head/0035832414.jpg",
            "https://images.xxapi.cn/images/head/0563046341.jpg",
            "https://images.xxapi.cn/images/head/3017352472.jpg",
            "https://images.xxapi.cn/images/head/1836949111.jpg",
            "https://images.xxapi.cn/images/head/6692632314.jpg",
            "https://images.xxapi.cn/images/head/5096795095.jpg",
            "https://images.xxapi.cn/images/head/1892082905.jpg",
            "https://images.xxapi.cn/images/head/8269375615.jpg",
            "https://images.xxapi.cn/images/head/3589808102.jpg",
            "https://images.xxapi.cn/images/head/7138665529.jpg",
            "https://images.xxapi.cn/images/head/8381401955.jpg",
            "https://images.xxapi.cn/images/head/4329422748.jpg",
            "https://images.xxapi.cn/images/head/4184173344.jpg",
            "https://images.xxapi.cn/images/head/3317277601.jpg",
            "https://images.xxapi.cn/images/head/9363260807.jpg",
            "https://images.xxapi.cn/images/head/9381754985.jpg",
            "https://images.xxapi.cn/images/head/2024882270.jpg",
            "https://images.xxapi.cn/images/head/3825614379.jpg",
            "https://images.xxapi.cn/images/head/0301709497.jpg",
            "https://images.xxapi.cn/images/head/2581786866.jpg",
            "https://images.xxapi.cn/images/head/5459218574.jpg",
            "https://images.xxapi.cn/images/head/8807259739.jpg",
            "https://images.xxapi.cn/images/head/6633085146.jpg",
            "https://images.xxapi.cn/images/head/3819884385.jpg",
            "https://images.xxapi.cn/images/head/8794018361.jpg",
            "https://images.xxapi.cn/images/head/1021989167.jpg",
            "https://images.xxapi.cn/images/head/3698852461.jpg",
            "https://images.xxapi.cn/images/head/4133168733.jpg",
            "https://images.xxapi.cn/images/head/7166279194.jpg",
            "https://images.xxapi.cn/images/head/2191049189.jpg",
            "https://images.xxapi.cn/images/head/1449711536.jpg",
            "https://images.xxapi.cn/images/head/5566338297.jpg",
            "https://images.xxapi.cn/images/head/9749521923.jpg",
            "https://images.xxapi.cn/images/head/0155419129.jpg",
            "https://images.xxapi.cn/images/head/1410796074.jpg",
            "https://images.xxapi.cn/images/head/6530846888.jpg",
            "https://images.xxapi.cn/images/head/8439247356.jpg",
            "https://images.xxapi.cn/images/head/0502486121.jpg",
            "https://images.xxapi.cn/images/head/9320506015.jpg",
            "https://images.xxapi.cn/images/head/6368520555.jpg",
            "https://images.xxapi.cn/images/head/4342449042.jpg",
            "https://images.xxapi.cn/images/head/4304292641.jpg",
            "https://images.xxapi.cn/images/head/8996716859.jpg",

    };
    private static final String[] BIOS = {
            "喜欢旅行和摄影",
            "热爱编程和游戏",
            "美食爱好者",
            "健身达人",
            "音乐和电影迷",
            "热衷于手工艺品和绘画",
            "崇尚自然，喜欢徒步和露营",
            "对科技产品充满热情",
            "追求时尚潮流的先锋",
            "钟情于阅读与写作",
            "探索未知，天文爱好者",
            "享受速度与激情的车迷",
            "醉心于历史文化的考古爱好者",
            "致力于环保和可持续生活方式",
            "痴迷于舞蹈与表演艺术"
    };

    private static final String[] CATEGORY = {
            "游戏",
            "游泳",
            "羽毛球",
            "CityWalk"
    };


    public static final String[] IMAGE_URLS = {
            "https://images.xxapi.cn/images/head/0035129704.jpg",
            "https://images.xxapi.cn/images/head/9199988744.jpg",
            "https://images.xxapi.cn/images/head/0948273779.jpg",
            "https://images.xxapi.cn/images/head/6437048042.jpg",
            "https://images.xxapi.cn/images/head/4398620744.jpg",
            "https://images.xxapi.cn/images/head/8695043755.jpg",
            "https://images.xxapi.cn/images/head/3347472359.jpg",
            "https://images.xxapi.cn/images/head/0035832414.jpg",
            "https://images.xxapi.cn/images/head/0563046341.jpg",
            "https://images.xxapi.cn/images/head/3017352472.jpg",
            "https://images.xxapi.cn/images/head/1836949111.jpg",
            "https://images.xxapi.cn/images/head/6692632314.jpg",
            "https://images.xxapi.cn/images/head/5096795095.jpg",
            "https://images.xxapi.cn/images/head/1892082905.jpg",
            "https://images.xxapi.cn/images/head/8269375615.jpg",
            "https://images.xxapi.cn/images/head/3589808102.jpg",
            "https://images.xxapi.cn/images/head/7138665529.jpg",
            "https://images.xxapi.cn/images/head/8381401955.jpg",
            "https://images.xxapi.cn/images/head/4329422748.jpg",
            "https://images.xxapi.cn/images/head/4184173344.jpg",
            "https://images.xxapi.cn/images/head/3317277601.jpg",
            "https://images.xxapi.cn/images/head/9363260807.jpg",
            "https://images.xxapi.cn/images/head/9381754985.jpg",
            "https://images.xxapi.cn/images/head/2024882270.jpg",
            "https://images.xxapi.cn/images/head/3825614379.jpg",
            "https://images.xxapi.cn/images/head/0301709497.jpg",
            "https://images.xxapi.cn/images/head/2581786866.jpg",
            "https://images.xxapi.cn/images/head/5459218574.jpg",
            "https://images.xxapi.cn/images/head/8807259739.jpg",
            "https://images.xxapi.cn/images/head/6633085146.jpg",
            "https://images.xxapi.cn/images/head/3819884385.jpg",
            "https://images.xxapi.cn/images/head/8794018361.jpg",
            "https://images.xxapi.cn/images/head/1021989167.jpg",
            "https://images.xxapi.cn/images/head/3698852461.jpg",
            "https://images.xxapi.cn/images/head/4133168733.jpg",
            "https://images.xxapi.cn/images/head/7166279194.jpg",
            "https://images.xxapi.cn/images/head/2191049189.jpg",
            "https://images.xxapi.cn/images/head/1449711536.jpg",
            "https://images.xxapi.cn/images/head/5566338297.jpg",
            "https://images.xxapi.cn/images/head/9749521923.jpg",
            "https://images.xxapi.cn/images/head/0155419129.jpg",
            "https://images.xxapi.cn/images/head/1410796074.jpg",
            "https://images.xxapi.cn/images/head/6530846888.jpg",
            "https://images.xxapi.cn/images/head/8439247356.jpg",
            "https://images.xxapi.cn/images/head/0502486121.jpg",
            "https://images.xxapi.cn/images/head/9320506015.jpg",
            "https://images.xxapi.cn/images/head/6368520555.jpg",
            "https://images.xxapi.cn/images/head/4342449042.jpg",
            "https://images.xxapi.cn/images/head/4304292641.jpg",
            "https://images.xxapi.cn/images/head/8996716859.jpg",
    };

    public static final List<User> moniUsers = new ArrayList<>();

    public static void generateUsers(int count) {
        List<User> users = new ArrayList<>();
        Random random = new Random();

        for (int i = 1; i <= count; i++) {
            User user = new User();
            user.setId(String.valueOf(i));
            user.setUsername("user" + i);
            user.setPassword("password" + i);
            user.setNickname(NICKNAMES[i]);
            user.setGender(GENDERS[random.nextInt(GENDERS.length)]);
            user.setCategory(CATEGORY[random.nextInt(CATEGORY.length)]);
            user.setAvatarUrl(AVATAR_URLS[random.nextInt(AVATAR_URLS.length)]);
            user.setBio(BIOS[random.nextInt(BIOS.length)]);
            user.setActive(random.nextBoolean());

            // 随机生成私人相册图片集
            int imageCount = random.nextInt(15) + 1; // 每人 1-5 张图片
            List<String> images = new ArrayList<>();
            for (int j = 0; j < imageCount; j++) {
                images.add(IMAGE_URLS[random.nextInt(IMAGE_URLS.length)]);
            }
            user.setImages(images);

            users.add(user);
        }

        // 通过单例模式 把数据存储到里面
        AllUserList allUserList = AllUserList.getInstance();
        insertUniqueUsers(allUserList.getSharedList(), users);
    }

    /**
     * 将第二个列表的用户插入到第一个列表中，确保 userId 不重复
     *
     * @param list1 第一个列表
     * @param list2 第二个列表
     */
    public static void insertUniqueUsers(List<User> list1, List<User> list2) {
        // 使用 HashSet 存储已有的 userId，提高查找效率
        HashSet<String> existingUserIds = new HashSet<>();
        for (User user : list1) {
            existingUserIds.add(user.getId());
        }

        // 遍历第二个列表，插入不重复的用户
        for (User user : list2) {
            if (!existingUserIds.contains(user.getId())) {
                list1.add(user); // 插入到第一个列表
                existingUserIds.add(user.getId()); // 更新已存在的 userId 集合
            }
        }
    }

    public static void clearUserList() {
        // 通过单例模式 把数据存储到里面
        AllUserList allUserList = AllUserList.getInstance();
        allUserList.setSharedList(new ArrayList<>());
    }


}